package com.prs.crm.action.assets;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.context.ToolKits;
import com.prs.crm.domain.approval.Application;
import com.prs.crm.domain.assets.AssetsBigClass;
import com.prs.crm.domain.assets.AssetsLingYong;
import com.prs.crm.service.approval.ApplicationService;
import com.prs.crm.service.assets.AssetsBigClassService;

public class AssetsBigClassAction extends BaseAction{
	private PaginationSupport pagination;
	private Integer[] ids;
	private AssetsBigClassService assetsBigClassService;
	private AssetsBigClass assetsBigClass;
	private Application application;
	private ApplicationService ApplicationService;
	public Application getApplication() {
		return application;
	}
	public void setApplication(Application application) {
		this.application = application;
	}
	public ApplicationService getApplicationService() {
		return ApplicationService;
	}
	public void setApplicationService(ApplicationService applicationService) {
		ApplicationService = applicationService;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public AssetsBigClassService getAssetsBigClassService() {
		return assetsBigClassService;
	}
	public void setAssetsBigClassService(AssetsBigClassService assetsBigClassService) {
		this.assetsBigClassService = assetsBigClassService;
	}
	public AssetsBigClass getAssetsBigClass() {
		return assetsBigClass;
	}
	public void setAssetsBigClass(AssetsBigClass assetsBigClass) {
		this.assetsBigClass = assetsBigClass;
	}
	//////////////////////////////////////////////////////////////////////////////
	@Action("listBigClass")
	public String listBigClass(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(AssetsBigClass.class);
		this.setPagination(this.assetsBigClassService.listByPage(criteria, 
				this.getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}
	@Action("editBigClass")
	public String editBigClass(){
		return SUCCESS;
	}
	@Action(value="saveBigClass",results={
			@Result(name="success",type="redirect",location="listBigClass"),
			@Result(name="error",type="chain",location="editBigClass")})
	public String saveBigClass(){
		if(!ToolKits.isInteger(assetsBigClass.getCanzhi().trim())){
			addActionError("残值率只能为整数！");
			return ERROR;
		}
		if (Integer.parseInt(assetsBigClass.getCanzhi())>100) {
			addActionError("残值率不能大于100！");
			return ERROR;
		}
		assetsBigClass.setRecorder(null);
		assetsBigClass.setRecordTime(null);
		assetsBigClass.setSign(null);
		this.assetsBigClassService.save(assetsBigClass);
//		application = new Application();
//		application.setType("一般");
//		application.setAppName("[一般]"+"资产总类");
//		application.setSqType(2);
//		application.setSpr(0);
//		application.setPerson(assetsBigClass.getRecorder());
//		application.setResign("资产总类");
//		application.setAssetsBigClass(assetsBigClass);
//		this.getApplicationService().save(application);
		return SUCCESS;
	}
	//保存并继续
	@Action(value="saveedBigClass",results={
			@Result(name="success",type="redirect",location="editBigClass"),
			@Result(name="error",type="chain",location="editBigClass")})
	public String saveedBigClass(){
		if(!ToolKits.isInteger(assetsBigClass.getCanzhi().trim())){
			addActionError("残值率只能为整数！");
			return ERROR;
		}
		if (Integer.parseInt(assetsBigClass.getCanzhi())>100) {
			addActionError("残值率不能大于100！");
			return ERROR;
		}
		assetsBigClass.setRecorder(null);
		assetsBigClass.setRecordTime(null);
		assetsBigClass.setSign(null);
		this.assetsBigClassService.save(assetsBigClass);
		return SUCCESS;
	}
	@Action(value="removeBigClass",results={
			@Result(name="success",type="redirect",location="listBigClass"),
			@Result(name="error",type="chain",location="listBigClass")})
	public String removeBigClass(){
		if (assetsBigClass.getAssetsSmallClasses().size()>0) {
			this.addActionError("已有小类关联，不能删除！");
			return ERROR;
		}
		if (assetsBigClass.getApplication().size()>0) {
			this.addActionError("该类已有关联，不能删除");
			return ERROR;
		}
		this.assetsBigClassService.remove(assetsBigClass);
		return SUCCESS;
	}
	@Action("viewBigClass")
	public String viewBigClass(){
		return SUCCESS;
	}
}
